package deptConfig

import (
	"context"
	"devops-super/api/configs/v1"
	"devops-super/internal/model/entity"
	"devops-super/internal/service"
	"github.com/gogf/gf/v2/os/gtime"
)

// GetPageLst 分页获取部门配置列表
func (*ControllerV1) GetPageLst(ctx context.Context, req *v1.DeptConfigGetPageLstReq) (res *v1.DeptConfigGetPageLstRes, err error) {
	res = &v1.DeptConfigGetPageLstRes{}
	res.PageLstRes, err = service.DeptConfig().GetPageLst(ctx, req.PageLstReq)
	return
}

// Add 添加部门配置
func (*ControllerV1) Add(ctx context.Context, req *v1.DeptConfigAddReq) (res *v1.DeptConfigAddRes, err error) {
	config := &entity.DeptConfig{
		DeptId:      int64(req.DeptId),
		GroupName:   req.GroupName,
		ConfigKey:   req.ConfigKey,
		ConfigValue: req.ConfigValue,
		Remark:      req.Remark,
		Range:       req.Range,
		CreatedAt:   gtime.Now(),
		UpdatedAt:   gtime.Now(),
	}

	id, err := service.DeptConfig().Add(ctx, config)
	if err != nil {
		return nil, err
	}

	return &v1.DeptConfigAddRes{Id: id}, nil
}

// Upt 更新部门配置
func (*ControllerV1) Upt(ctx context.Context, req *v1.DeptConfigUptReq) (res *v1.DeptConfigUptRes, err error) {
	config := &entity.DeptConfig{
		Id:          int64(req.Id),
		DeptId:      int64(req.DeptId),
		GroupName:   req.GroupName,
		ConfigKey:   req.ConfigKey,
		ConfigValue: req.ConfigValue,
		Remark:      req.Remark,
		Range:       req.Range,
		UpdatedAt:   gtime.Now(),
	}

	err = service.DeptConfig().Upt(ctx, config)
	return
}

// Del 删除部门配置
func (*ControllerV1) Del(ctx context.Context, req *v1.DeptConfigDelReq) (res *v1.DeptConfigDelRes, err error) {
	err = service.DeptConfig().Del(ctx, req.Id)
	return
}

// GetOne 获取单个部门配置
func (*ControllerV1) GetOne(ctx context.Context, req *v1.DeptConfigGetOneReq) (res *v1.DeptConfigGetOneRes, err error) {
	info, err := service.DeptConfig().GetOne(ctx, req.Id)
	if err != nil {
		return nil, err
	}

	return &v1.DeptConfigGetOneRes{Info: info}, nil
}

// GetByDept 按部门获取配置
func (*ControllerV1) GetByDept(ctx context.Context, req *v1.DeptConfigGetByDeptReq) (res *v1.DeptConfigGetByDeptRes, err error) {
	configs, err := service.DeptConfig().GetByDept(ctx, req.DeptId, req.GroupName)
	if err != nil {
		return nil, err
	}
	return &v1.DeptConfigGetByDeptRes{Configs: configs}, nil
}

// GetGroupConfigs 获取部门配置组
func (*ControllerV1) GetGroupConfigs(ctx context.Context, req *v1.GetDeptGroupConfigsReq) (res *v1.GetDeptGroupConfigsRes, err error) {
	res, err = service.DeptConfig().GetGroupConfigs(ctx, req)
	return
}

// SetGroupConfigs 保存部门配置组
func (*ControllerV1) SetGroupConfigs(ctx context.Context, req *v1.SetDeptGroupConfigsReq) (res *v1.SetDeptGroupConfigsRes, err error) {
	res = &v1.SetDeptGroupConfigsRes{}
	err = service.DeptConfig().SetGroupConfigs(ctx, req)
	return
}
